<template>
  <div id="app">
    <!--<img src="./assets/logo.png">-->
    <router-view/>
  </div>
</template>

<script>
  // css
  import "./assets/css/reset.css";
  import "./assets/css/style.css";

  // js
  import './assets/js/jquery-3.3.1'
  import './assets/js/jquery.form'
  import './assets/js/bootstrap.min'

  import router from './router'

  export default {
    name: 'App',
    methods: {
      checkLogin: function () {
        let _this = this;
        let token = _this.getCookie("token");
        if (token !== null && token !== "" && this.$route.path === '/Login') {
          router.push({
            path: '/'
          })
        }
      },
      checkExpireTime() {
        // 检查token是否过期
        let _this = this;
        let expire_time = _this.getCookie("expire_time");
        let d = new Date();
        d.setTime(d.getTime());
        let expires = d.getTime();
        expires = "" + expires;
        expires = expires.slice(0, 10);

        // 打印两个相比较的时间
        // console.log(expires);
        // console.log(expire_time);
        if (expires > expire_time) {
          _this.clearCookie();
          router.push({
            path: '/Login'
          })
        }
      },
    },
    created() {
      let _this = this;
      _this.checkExpireTime();
    },
    mounted: function () {
      let _this = this;
      _this.checkLogin();
    },
    watch: {
      // 检查url是否变化
      "$route": function (val, oldVal) {
          // 检查token存在，是否为'/Login'
          let _this = this;
          let token = _this.getCookie("token");
          if (token && val.fullPath === '/Login') {
            router.push({
              path: '/'
            })
          }
          _this.checkExpireTime();
      },
      // 深度观察监听
      deep: true
    },
  }
</script>

<style>
</style>
